package org.dydl.service.system.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IOrderDetailRepository;
import org.dydl.dao.system.repository.IOrderRepository;
import org.dydl.model.system.OrderDetail;
import org.dydl.model.system.Orders;
import org.dydl.model.system.vo.OrdersVO;
import org.dydl.service.system.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private IOrderRepository orderRepository;
	@Autowired
	private IOrderDetailRepository orderDeteailRepository;
	
	private ResponseMessage res = new ResponseMessage();

	/**
	 * 查询订单列表
	 * @param page
	 * @param rows
	 * @param orderNo
	 * @return
	 */
	@Override
	public ResponseMessage list(int page, int rows, String orderNo){
  		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Orders> pageList = null;
		if (StringUtils.isNotBlank(orderNo)) {
			pageList = orderRepository.getOrdersListLike(String.format("%s%s%s", "%", StringUtils.trim(orderNo), "%"),pageable);
		} else {
			pageList = orderRepository.getOrdersList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<Orders>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
	}

	/**
	 * 提交订单
	 * @param ordersVO
	 * @return
	 */
	@Override
	@Transactional
	public ResponseMessage bothSave(OrdersVO ordersVO) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String uuid = UUID.randomUUID().toString();
			ordersVO.getOrders().setUuid(uuid);
			if (ordersVO.getOrders().getCreateDate() == null) {
				ordersVO.getOrders().setCreateDate(Timestamp.valueOf(df.format(new Date())));
			}
			orderRepository.save(ordersVO.getOrders());
			List<OrderDetail> arrList = ordersVO.getArr();
			for (OrderDetail orderdetail : arrList) {
				uuid = UUID.randomUUID().toString();
				orderdetail.setUuid(uuid);
				orderDeteailRepository.save(orderdetail);
			}
			res.setSuccess(true);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 删除
	 * @param id
	 */
	@Override
	public void delete(int id) {
		orderRepository.delete(id);
	}

	/**
	 * 查询订单明细列表
	 * @param orderNo
	 * @return
	 */
	@Override
	public List<OrderDetail> detailList(String orderNo){
		return orderDeteailRepository.getOrderDetailList(orderNo);
	}

	/**
	 * 审核订单
	 * @param id
	 */
	@Override
	public ResponseMessage examineOrder(int id) {
		
		try {
			orderRepository.changeExamineStatus(id);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}

	

	/**
	 * 根据订单号查询订单
	 * @param orderNo
	 * @return
	 */
	@Override
	public ResponseMessage findByOrderNo(String orderNo) {
		
		try {
			Orders orders =  orderRepository.findByOrderNo(orderNo);
			res.setSuccess(true);
			res.setData(orders);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}
}
